<template>
  <div>
    <div style="width: 90%; margin: 0 auto">
      <div class="search-box">
        <!-- 学历 -->
        <div class="search-left">
          <el-select
            v-model="edu"
            placeholder="请选择学历"
            style="width: 150px; display: inline-block; margin-right: 5px"
          >
            <el-option label="全部学历" value="全部学历"></el-option>
            <el-option label="本科生毕业" value="本科生毕业"></el-option>
            <el-option label="研究生毕业" value="研究生毕业"></el-option>
          </el-select>
          <!-- 毕业年份 -->
          <el-select
            v-model="gradTime"
            placeholder="请选择毕业年份"
            style="width: 150px; display: inline-block; margin-right: 5px"
          >
            <el-option
              v-for="item in years"
              :key="item"
              :label="item != 0 ? item : '全部年份'"
              :value="item"
            ></el-option>
          </el-select>
          <el-button
            type="primary"
            @click="handleFliter"
            style="margin-right: 50px"
            >筛选</el-button
          >
        </div>
        <div class="search-right">
          <div style="flex: 2">
            <el-input
              placeholder="考生号或学号,为空则重新加载列表"
              v-model="xid"
              style="width: 300px; margin-right: 5px"
              class="input-with-select"
              @keyup.enter.native="handleSearch"
            >
              <el-button
                slot="append"
                icon="el-icon-search"
                @click="handleSearch"
              ></el-button>
            </el-input>
          </div>
          <div style="flex: 3">
            <a
              href="./template.xlsx"
              download="毕业去向上传模板.xlsx"
              class="el-button el-button--plain el-button-medium"
              >下载模板</a
            >
            <el-button type="plain" @click="handleExcel">上传excel</el-button>
            <el-button type="primary" @click="handleAdd"
              >新建毕业去向</el-button
            >
          </div>
        </div>
      </div>
      <div style="margin-top: 10px; margin-bottom: 10px">
        <el-table ref="table" :data="tableData" border v-loading="listLoading">
          <el-table-column prop="id" label="考生号" width="150">
          </el-table-column>
          <el-table-column prop="sex" label="性别" width="100">
          </el-table-column>
          <el-table-column prop="nation" label="民族" width="100">
          </el-table-column>
          <el-table-column prop="politicsSt" label="政治面貌" width="100">
          </el-table-column>
          <el-table-column prop="school" label="院校名称" width="120">
          </el-table-column>
          <el-table-column prop="eduBackgro" label="学历" width="120">
          </el-table-column>
          <el-table-column prop="major" label="专业" width="120">
          </el-table-column>
          <el-table-column prop="origin" label="生源所在地" width="120">
          </el-table-column>
          <el-table-column prop="gradTime" label="毕业时间" width="120">
          </el-table-column>
          <el-table-column prop="difficulty" label="困难生类别" width="120">
          </el-table-column>
          <el-table-column prop="department" label="所在院系" width="120">
          </el-table-column>
          <el-table-column prop="sid" label="学号" width="120">
          </el-table-column>
          <el-table-column prop="direction" label="毕业去向" width="160">
          </el-table-column>
          <el-table-column prop="orgName" label="单位名称" width="200">
          </el-table-column>
          <el-table-column prop="orgKind" label="单位性质" width="120">
          </el-table-column>
          <el-table-column prop="orgIndustry" label="单位行业" width="240">
          </el-table-column>
          <el-table-column prop="orgLocation" label="单位所在地" width="140">
          </el-table-column>
          <el-table-column prop="jobType" label="工作职位类别" width="140">
          </el-table-column>
          <el-table-column prop="orgAddress" label="单位地址" width="120">
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="100">
            <template slot-scope="scope">
              <el-button @click="handleEdit(scope.row)" type="text"
                >编辑</el-button
              >
              <el-button
                @click="handleDelete(scope.row)"
                type="text"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          style="text-align: center; margin-top: 10px; margin-bottom: 50px"
          @current-change="handleCurrentChange"
          :current-page.sync="currentPage"
          :page-size="pageSize"
          layout="total, prev, pager, next,jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<style scoped>
.search-box {
  margin-top: 15px;
  width: 100%;
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: flex-end;
}

.search-left {
  flex: 2;
}

.search-right {
  flex: 3;
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: flex-end;
}
</style>

<script>
import UploadExcelComponent from '@/components/UploadExcel/index.vue'
import { getGradList, searchGradDirection, deleteGradDirection, getYears } from '@/api/graduation.js'

export default {
  components: { UploadExcelComponent },
  data() {
    return {
      edu: '',//1本科生毕业，2研究生毕业
      gradTime:'',//毕业时间，年份
      listLoading: true,
      tableData: [{
        id: "1",
        sex: "1",
        nation: "1",
        politicsSt: "1",
        school: "1",
        eduBackgro: "1",
        major: "1",
        origin: "1",
        gradTime: "1",
        difficulty: "1",
        department: "1",
        sid: "1",
        direction: "1",
        orgName: "1",
        orgKind: "",
        orgIndustry: "",
        orgLocation: "",
        jobType: "",
        orgAddress: ""
      }],
      xid: '',//考生号或学号
      currentPage: 1,//当前所处的页数
      pageSize: 20,//每页多少条数据
      total: 1,//数据总数
      years: [],
    }
  },
  created() {
    this.getYears()
    this.getList()
  },
  methods: {
    getYears() {
      getYears().then(response => {
        this.years = response.data
        this.years.unshift(0);
        console.log(this.years)
      }).catch(err => {
        console.log(err)
      })
    },
    getList() {
      this.listLoading = true
      let year=this.gradTime==''?0:this.gradTime
      let data = {
        start: year,
        end: year,
        edu: this.edu,
        pageCount: this.currentPage,
        size: this.pageSize
      }
      getGradList(data).then(response => {
        this.total = response.data.total
        this.tableData = response.data.list
        this.listLoading = false
      }).catch(err => {
        console.log(err)
      })
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getList()
    },
    handleSearch() {//根据学号or考生号搜索毕业去向信息
      if (this.xid == '') {
        this.getList()
      } else {
        this.listLoading = true
        let data = { xid: this.xid }
        searchGradDirection(data).then(response => {
          //学历和年份清空
          this.years = []
          this.edu = ''
          this.gradTime = 0
          //xid清空
          this.xid = ''
          this.total = 1
          this.currentPage = 1
          this.tableData = []
          this.tableData.push(response.data)
          this.listLoading = false
        }).catch(err => {
          console.log(err)
          this.xid=""
          this.getList()

        })
      }
    },
    handleFliter() {//根据毕业年份和筛选
      this.getList()
    },
    handleAdd() {//添加毕业去向
      this.$router.push('/graduation/create')
    },
    handleExcel() {//上传excel跳转到该路由
      this.$router.push('/graduation/uploadExcel')
    },
    handleEdit(row) {
      this.$router.push({
        path: '/graduation/create',
        name:'createGraduation',
        params: {
          dataObj: row
        }
      })
    },
    handleDelete(row) {
      this.$confirm('毕业去向信息很重要！您确定要删除该条信息嘛？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let data = { id: row.id }
        deleteGradDirection(data).then(response => {
          this.$message({
            type: 'success',
            message: '考生号为: ' + row.id + ' 的毕业去向数据删除成功'
          })
          this.getList()
        }).catch(err => {
          console.log(err)
        })
      }).catch(() => {
        //do nothing
      });
    }
  }
}
</script>


